# coding: utf-8
# -----------------------
# @project：GamesPlugIn
# @Author ： lizheyan
# @File ：PvZ.py
# @Desc:
# @Time ： 2022/07/27 03:44:37
# -----------------------

import pymem
import sys
import time

import requests
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QMainWindow, QApplication
from pyz_game import Ui_MainWindow


class PyZGameUi(Ui_MainWindow, QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.update_sun)
        self.pushButton_2.clicked.connect(self.update_money)

    def update_sun(self) -> None:
        try:
            sun_value = int(self.lineEdit.text())
            PyZGame().update_sun(sun_value)
        except:
            print("输入不合法走默认修改值")
            PyZGame().update_sun()

    def update_money(self):
        try:
            money_value = int(self.lineEdit_2.text())
            PyZGame().update_money(money_value)
        except:
            pass


class PyZGame:
    def __init__(self):
        self.game_obj = pymem.Pymem('PlantsVsZombies.exe')  # 拿到操作对象
        self.base_addr = self.game_obj.read_int(0x779670)  # 0x779670是基址，在ce中基址是标绿色的，有多个任意选择一个即可

    def update_sun(self, sun_value=8888):
        """
        修改阳光值
        :param sun_value: 
        :return: 
        """""
        sun_addr = self.game_obj.read_int(self.base_addr + 0x868)
        sun_values = self.game_obj.read_int(sun_addr + 0x5578)
        print('未修改前的阳光数：', sun_values)
        self.game_obj.write_int(sun_addr + 0x5578, sun_value)
        get_update_sun_values = self.game_obj.read_int(sun_addr + 0x5578)
        print('修改后的阳光数：', get_update_sun_values)

    def update_money(self, money: int):
        """
        修改金币
        :param money: 游戏中的金币值 = money * 10
        :return:
        """
        one_addr = self.game_obj.read_int(self.base_addr + 0x950)
        money_addr = self.game_obj.read_int(one_addr + 0x50)
        print("修改前的金币：", money_addr * 10)
        self.game_obj.write_int(one_addr + 0x50, money)
        get_update_money_values = self.game_obj.read_int(one_addr + 0x50)
        print('修改后的金币：', get_update_money_values * 10)

    def __del__(self):
        self.game_obj.close_process()  # 关闭操作对象


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWin = PyZGameUi()
    myWin.show()
    sys.exit(app.exec_())

